package com.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.store.domain.R;
import com.store.domain.entity.Book;
import com.store.domain.entity.Shopcar;
import com.store.domain.vo.ShopcarVo;
import com.store.mapper.BookMapper;
import com.store.mapper.ShopcarMapper;
import com.store.service.ShopcarService;
import com.store.utils.BeanCopyUtils;
import com.store.utils.ThreadLocalUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 购物车表(Shopcar)表服务实现类
 *
 * @author yu
 * @since 2024-06-26 01:12:42
 */
@Service("shopcarService")
public class ShopcarServiceImpl extends ServiceImpl<ShopcarMapper, Shopcar> implements ShopcarService {

    @Resource
    private BookMapper bookMapper;
    @Transactional
    @Override
    public R addShopCar(Integer bookId, Integer number) {
        Integer userId = ThreadLocalUtil.getLocal();
        LambdaQueryWrapper<Shopcar> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Shopcar::getBookId,bookId);
        queryWrapper.eq(Shopcar::getUserId, userId);
        if (getOne(queryWrapper)!=null){
            return R.error("已经在购物车中了");
        }
        Shopcar shopcar = new Shopcar();
        shopcar.setBookId(bookId);
        shopcar.setNum(number);
        shopcar.setUserId(userId);
        //减少
        Book book = bookMapper.selectById(bookId);
        book.setStoreNum(book.getStoreNum()-number);
        bookMapper.updateById(book);
        boolean save = save(shopcar);
        if (save){
            return R.success("加入购物车成功");
        }
        return R.error("加入购物车失败");
    }

    @Override
    public R getUserShopCar() {
        Integer userId = ThreadLocalUtil.getLocal();
        LambdaQueryWrapper<Shopcar> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Shopcar::getUserId,userId);
        List<Shopcar> shopCarList = list(queryWrapper);
        List<ShopcarVo> shopcarVos = BeanCopyUtils.copyBeanList(shopCarList, ShopcarVo.class);
        for (ShopcarVo shopcarVo : shopcarVos){
            Book book = bookMapper.selectById(shopcarVo.getBookId());
            shopcarVo.setName(book.getName());
            shopcarVo.setPrice(book.getPrice());
            shopcarVo.setTotalPrice(shopcarVo.getNum()*book.getPrice());
        }
        return R.success(shopcarVos);
    }

    @Override
    public R updateShopCar(Integer id, Integer num) {
        Shopcar shopcar = getById(id);
        shopcar.setNum(num);
        boolean b = updateById(shopcar);
        if (b){
            return R.success("增加数量成功");
        }
        return R.error("增加数量失败");
    }

    @Override
    public R deleteShopCars(Integer[] ids) {
        boolean b = removeByIds(Arrays.asList(ids));
        if (b){
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }
}


